package com.training.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.training.common.Result;
import com.training.common.TrainingPage;
import com.training.common.model.Role;
import com.training.common.model.User;
import com.training.service.RoleService;
import com.training.service.RoleServiceImp;

import java.util.Date;

public class RoleController extends Controller {

    @Inject(RoleServiceImp.class)
    private RoleService roleService;

    public void index(){
        render("/views/role/index.html");
    }

    public void getRolePage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<Role> menuList = roleService.getRolePage(pageNumber,pageSize,name);
        Result result = new Result();
        result.setData(new TrainingPage(menuList));
        renderJson(result);
    }

    public void showRole(){
        if(getPara("id")==null){
            render("/views/role/add.html");
        }else{
            String id = getPara("id");
            setAttr("id",id);
            render("/views/role/edit.html");
        }

    }

    public void addRole(){
        String name = getPara("name");
        Role role = new Role();
        role.setName(name);
        User user = getSessionAttr("trainingsession");
        role.setUserId(user.getID());
        role.setCreatetime(new Date());
        boolean boo = roleService.addRole(role);
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void getRole(){
        String id = getPara("id");
        Role role = Role.dao.findById(id);
        Result result = new Result();
        result.setData(role);
        renderJson(result);
    }

    public void editRole(){
        String id = getPara("id");
        String name = getPara("name");
        Result result = new Result();
        if(id.equals("1")){
            result.setErrorNo(500);
            result.setErrorInfo("更新失败,管理员角色不允许修改！");
        }else {
            Role role = Role.dao.findById(id);
            role.setName(name);
            boolean boo = roleService.updateRole(role);

            if (boo) {
                result.setErrorNo(0);
                result.setErrorInfo("更新成功！");
            } else {
                result.setErrorNo(500);
                result.setErrorInfo("更新失败！");
            }
        }
        renderJson(result);
    }

    @Before(Tx.class)
    public void deleteRole(){
        String ids = getPara("id");
        String[] idss = ids.split(",");
        boolean ifboo = true;
        for (String s : idss) {
            if (s.equals("1")){
                ifboo = false;
            }
        }
        Result result = new Result();
        if(ifboo){
            boolean boo = false;
            for (String id : idss){
                int idi = Integer.parseInt(id);
                boo = roleService.deleteRole(idi);
            }
            if(boo){
                result.setErrorNo(0);
                result.setErrorInfo("删除成功！");
            }else{
                result.setErrorNo(500);
                result.setErrorInfo("删除失败！");
            }
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败，管理员角色不允许删除！");
        }
        renderJson(result);
    }

    public void showmenutree(){
        setAttr("roleid",getPara("id"));
        render("/views/role/accredit.html");
    }

    @Before(Tx.class)
    public void accredit(){
        String menuIds[] = getParaValues("menuIds[]");
        String roleid = getPara("roleid");
        Result result = new Result();
        if(roleid.equals("1")){
            result.setErrorNo(500);
            result.setErrorInfo("授权失败,管理员角色不允许修改授权！");
        }else {
            boolean boo = roleService.accredit(roleid,menuIds);
            if(boo){
                result.setErrorNo(0);
                result.setErrorInfo("授权成功！");
            }else{
                result.setErrorNo(500);
                result.setErrorInfo("授权失败！");
            }
        }
        renderJson(result);
    }
}
